﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TouchUI;

namespace Sample
{
    public partial class MainForm : Form
    {
        private List<Dialog> dialogStack;
        private Timer timer;
        private bool loaded = false;
        private int animationLeft;
        private int animationOffset;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            dialogStack = new List<Dialog>();
            MainDialog mainDialog = new MainDialog(Common.Instance.ScreenFactor, Common.Instance.ClientRectangle);
            mainDialog.Selected += new EventHandler(dialogSelected);
            dialogStack.Add(mainDialog);
            navigate();

            timer = new Timer();
            timer.Interval = 80; // 12.5 fps
            timer.Tick += new EventHandler(timer_Tick);
            timer.Enabled = true;

            loaded = true;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.Refresh();
        }

        private void MainForm_Activated(object sender, EventArgs e)
        {
            if(loaded)
                timer.Enabled = true;
        }

        private void MainForm_Deactivate(object sender, EventArgs e)
        {
            if(loaded)
                timer.Enabled = false;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if(!loaded)
                return;

            dialogStack.Last().Paint(Common.Instance.PaintGraphics);
            Rectangle r = Common.Instance.ClientRectangle;
            if(animationLeft != 0)
            {
                e.Graphics.DrawImage(Common.Instance.PaintCopyBitmap,
                    new Rectangle(animationLeft - r.Width * animationLeft.CompareTo(0), 0, r.Width, r.Height),
                    r, GraphicsUnit.Pixel);
                e.Graphics.DrawImage(Common.Instance.PaintBitmap, new Rectangle(animationLeft, 0, r.Width, r.Height), r, GraphicsUnit.Pixel);
                animationLeft -= animationOffset;
                if(Math.Abs(animationLeft) < Math.Abs(animationOffset))
                    animationLeft = 0;
            }
            else
                e.Graphics.DrawImage(Common.Instance.PaintBitmap, r, r, GraphicsUnit.Pixel);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
        }

        private void dialogSelected(object sender, EventArgs e)
        {
            this.Refresh();
            navigateForward();
        }

        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            dialogStack.Last().MouseDown(new Point(e.X, e.Y));
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            dialogStack.Last().MouseMove(new Point(e.X, e.Y));
        }

        private void MainForm_MouseUp(object sender, MouseEventArgs e)
        {
            dialogStack.Last().MouseUp(new Point(e.X, e.Y));
        }

        private void navigateForward()
        {
            timer.Enabled = false;
            Cursor.Current = Cursors.WaitCursor;
            Common.Instance.PaintCopyGraphics.DrawImage(Common.Instance.PaintBitmap, 0, 0);

            if(dialogStack.Last() is MainDialog)
            {
                MainDialog mainDialog = (MainDialog)dialogStack.Last();
                DetailDialog detailDialog = new DetailDialog(Common.Instance.ScreenFactor, Common.Instance.ClientRectangle, mainDialog.SelectedLine + 1);
                dialogStack.Add(detailDialog);
                navigate();
            }

            animationOffset = Common.Instance.ClientRectangle.Width / 4;
            animationLeft = Common.Instance.ClientRectangle.Width;
            Cursor.Current = Cursors.Default;
            timer.Enabled = true;
        }

        private void navigateBack()
        {
            timer.Enabled = false;
            Cursor.Current = Cursors.WaitCursor;
            Common.Instance.PaintCopyGraphics.DrawImage(Common.Instance.PaintBitmap, 0, 0);

            dialogStack.Remove(dialogStack.Last());
            navigate();

            animationOffset = -Common.Instance.ClientRectangle.Width / 4;
            animationLeft = -Common.Instance.ClientRectangle.Width;
            Cursor.Current = Cursors.Default;
            timer.Enabled = true;
        }

        private void navigate()
        {
            if(dialogStack.Last() is MainDialog)
                actionMenuItem.Text = "Select";
            if(dialogStack.Last() is DetailDialog)
                actionMenuItem.Text = "Back";

            if(!Common.Instance.TouchDevice)
                this.Text = dialogStack.Last().Text;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            Common.Instance.ClientRectangle = Screen.PrimaryScreen.WorkingArea;
            if(loaded)
                foreach(Dialog dialog in dialogStack)
                    dialog.Resize(Common.Instance.ClientRectangle);
            this.Refresh();
        }

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Back && actionMenuItem.Text == "Back")
                navigateBack();
            else
                dialogStack.Last().KeyUp(e);
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            dialogStack.Last().KeyDown(e);
        }

        private void actionMenuItem_Click(object sender, EventArgs e)
        {
            if(actionMenuItem.Text == "Select")
                navigateForward();
            else if(actionMenuItem.Text == "Back")
                navigateBack();
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}